<?php
if (!isset($_GET['e']) || !isset($_GET['i']))
	exit;

include_once 'function.php';

$email = $_GET['e'];
$active_key = $_GET['i'];
$conn = conn();
$esc_email = mysql_real_escape_string($email, $conn);
$esc_active_key = mysql_real_escape_string($active_key, $conn);
$sql = "SELECT id, active, active_key FROM accounts WHERE email = '$esc_email'";
$result = mysql_query($sql, $conn);
if ($row = mysql_fetch_assoc($result))
{
	if ($active_key == $row['active_key'] || 1 == $row['active'])
	{
		// login automatically
		$_SESSION['id'] = $row['id'];

		$meta = '<meta http-equiv="refresh" content="5; url=/" />';
		if ($active_key == $row['active_key'])
		{
			// active the account and jump to index
			$sql = "UPDATE accounts SET active = 1, active_key = NULL WHERE email = '$esc_email'";
			mysql_query($sql, $conn);
			
			$msg = '您的账户激活成功！';
		}
		else
		{
			$msg = '您已经激活了您的账户。';
		}
		$msg .= '<br />5秒钟之内自动跳转到首页。<br />您也可以点击<a href="/">这里</a>手动跳转。';
		
	}
	else
	{
		$msg = '激活码错误';
	}
}
else 
{
	$msg = '邮箱不存在';

}
?><!DOCTYPE HTML>
<html>
	<head>
		<link rel="shortcut icon" href="favicon.ico" type="image/vnd.microsoft.icon" />
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<?php if(isset($meta)) echo $meta;?>
		<meta name="keywords" content="" />
		<meta name="description" content="" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="theme.css" />
	</head>
	<body>
		<div class="wrapper">
			<div style="text-align:center;">
				<?php echo $msg;?>
			</div>
		</div>
	</body>
</html>